package com.cyzy.controller;

import com.cyzy.bean.SMBean;
import com.cyzy.domain.MessageBean;
import com.cyzy.domain.TUser;
import com.cyzy.dto.ResponseData;
import com.cyzy.sendMessage.SendSms;
import com.cyzy.service.TUserService;


import com.cyzy.util.CodeUtile;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class TUserController {
    @Autowired
    JavaMailSender javaMailSender;


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TUserService userService;

    @RequestMapping("/getBase64Code")
    public ResponseData getBase64Code() {
        CodeUtile codeUtile=new CodeUtile();
        String code = codeUtile.getRandomString().toLowerCase(); // 转换为小写
        String base64 = codeUtile.getImageOfBase64(code);
        String uuid = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(uuid, code, 70, TimeUnit.SECONDS);
        Map<String, String> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("code", base64);
        ResponseData responseData = new ResponseData(0, "ok", map, 0);
        return responseData;
    }

    @PostMapping("reg")
    public ResponseData register(@RequestBody TUser tUser) {
        System.out.println(tUser);
        String code=redisTemplate.opsForValue().get(tUser.getUserPhone());
        if (code==null||code==""){
            return new ResponseData(0,"false",null,0);
        }else {
            if (tUser.getCode().equals(code)){
                return userService.register(tUser);
            }else{
                return new ResponseData(0,"false1",null,0);
            }

        }

    }
//    @GetMapping ("/Login")
//    public ResponseData Login(String phone,String password,Integer pwdOrMsg) {
//        //0是密码1是验证码
//        if (pwdOrMsg==0){
//            QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("phone",phone)//可以设置查询的条件
//            TUser tUser=
//        }else {
//
//        }
//    }
    @GetMapping ("/codeMessage")
    public ResponseData test1(String number) throws Exception {
        SMBean smBean=new SMBean();
        smBean.setNumber(number);
        smBean.setCode("SMS_154950909");
        smBean.setSign("阿里云短信测试");
        SendSms sendSms=new SendSms();
        String code=generateRandomCode(6);
        smBean.setRandomCode(code);
        System.out.println(code+"验证码");
        sendSms.messageSend(smBean);
        redisTemplate.opsForValue().set(number,code,70, TimeUnit.SECONDS);
        return new ResponseData(0,"success",0,0);
    }
    private String generateRandomCode(int length) {
        String characters = "0123456789";
        Random random = new SecureRandom();
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(random.nextInt(characters.length()));
        }
        return new String(text);
    }

    @GetMapping ("/Login")
    public ResponseData Login(String phone,String password) throws Exception {

        return userService.login(phone,password);
    }

    @GetMapping ("/UUIDLogin")
    public ResponseData UUIDLogin(String phone,String password,String uuid,String code) throws Exception {
        return userService.UUIDLogin(phone,password,uuid,code);
    }

    @GetMapping ("/updatePassword")
    public ResponseData updatePassword(String phone,String password,String code) throws Exception {
        return userService.updatePassword(phone,password,code);
    }

    @GetMapping ("/sendEmail")
    public ResponseData sendEmail(String email) throws Exception {
        // 创建一个邮件消息
        MimeMessage message = javaMailSender.createMimeMessage();
        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(message, false);
        String code=generateRandomCode(6);
        // 发件人邮箱和名称
        helper.setFrom("1020461754@qq.com", "1020461754");
        // 收件人邮箱
        helper.setTo(email);
        redisTemplate.opsForValue().set(email,code,60,TimeUnit.SECONDS);
        // 邮件标题
        helper.setSubject("验证码");
        // 邮件正文，第二个参数表示是否是HTML正文
        helper.setText("验证码为："+code, false);
        // 发送
        javaMailSender.send(message);
        return new ResponseData(0,"success",null,0);
    }

    @GetMapping ("/emailLogin")
    public ResponseData emailLogin(String email,String emailCode) throws Exception {
        return userService.emailLogin(email,emailCode);
    }

    //查找用户信息

    @GetMapping("/getuser")
    public ResponseData getuser(TUser tUser){
        return userService.getUser(tUser);
    }
    //用户修改密码

    @GetMapping("/setPassword")
    public ResponseData setPassword(Integer userId,String oldpassword,String newpassword){
       return userService.setpassword(userId,oldpassword,newpassword);
    }
    //修改用户信息

    @GetMapping("/setuser")
    public int setuser(TUser tUser){
        return userService.setUser(tUser);
    }
    @GetMapping("/updateEditlnfoUser")
    public ResponseData updateEditlnfoUser(String phone ,String code ,String oldPassword ,String newPassword){
        return userService.updateEditlnfoUser(phone,code,oldPassword,newPassword);
    }
    @PostMapping("/svaeMessage")
    public ResponseData svaeMessage(@RequestBody MessageBean messageBean){
        return userService.svaeMessage(messageBean);
    }
}
